package syn;

import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
	public static void main(String[] args) {
		maipiao mai = new maipiao();
		new Thread(mai, "2").start();
		new Thread(mai, "1").start();
		new Thread(mai, "3").start();

	}
}

class maipiao implements Runnable {
	private int piao = 100;
	private final ReentrantLock lock = new ReentrantLock();

	@Override
	public void run() {
		while (true) {
			try {
				lock.lock();
				if (piao > 0) {
					System.out.println(Thread.currentThread().getName() + "====" + piao--);
				} else {
					break;
				}
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

			} finally {
				lock.unlock();
			}
		}
	}
}